import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import pandas as pd
data = pd.read_csv('../考核/data2.csv')
# TODO Task1
print(data.shape)
print(data.head(n = 10))
print(data.info())
data.loc[:,'Freedom'] = data.fillna(data.loc[:,'Freedom'].mean())
data.loc[:,'Family'] = data.fillna(data.loc[:,'Family'].mean())
data.loc[:,'Generosity'] = data.fillna(data.loc[:,'Generosity'].mean())
# TODO Task2
mean_1 = data['Economy'].mean()
mid_1 = data['Family'].median()
max_1 = data['Health'].max()
min_1 = data['Freedom'].min()
std_1 = data['Trust'].std()
print(mean_1)
print(mid_1)
print(max_1)
print(min_1)
print(std_1)
max_min = MinMaxScaler((0,10))
data_1 = max_min.fit_transform(data[['Economy','Family','Health','Freedom','Trust','Generosity']])
data_1 = np.mean(data_1,axis=1)
data_1 = data_1+data['Dystopia Residual']
# data['Happiness Score'] = data_1.astype(int)
data.insert(2,'Happiness Score',data_1)
# TODO Task3
ranked = data['Happiness Score'].sort_values(ascending=False).index[0:10]
find_1 = data.loc[ranked,['Country','Region','Happiness Score']]
print(find_1)
happy = data['Happiness Score'].mean()
eco = data['Economy'].mean()
find_2 = data.loc[(data.loc[:,'Happiness Score']>happy)&(data.loc[:,'Economy']>eco),'Country']
print(find_2)
# TODO Task4
new_data = data.groupby(['Region']).agg({'Country':"count",'Happiness Score':"mean"})
print(new_data)
fig = plt.figure(figsize=(30,10))
plt.bar(new_data.index,new_data['Happiness Score'],color='b')
plt.show()
# TODO Task5
print("Australia and New Zealand的国家最少但人们最幸福，Sub-Saharan Africa的国家最多但最不幸福，Western Europe有着全部区域幸福指数前十最多的国家")









